package com.jiyuren.wheelview.dialog;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.jiyuren.wheelview.PickViewConst;
import com.jiyuren.wheelview.R;
import com.jiyuren.wheelview.lib.TimeType;
import com.jiyuren.wheelview.utils.ScreenInfo;
import com.jiyuren.wheelview.view.WheelTime;

/**
 * com.pickerview.dialog.CommonTimeDialog
 * 
 * @author jiyuren <br/>
 *         create at 2015年6月6日 下午4:10:37
 */
public class CommonTimeDialog extends Dialog implements android.view.View.OnClickListener {
  private static final String TAG = "CommonTimeDialog";

  private View rootView;
  WheelTime wheelTime;
  private Button btnSubmit, btnCancel;
  private static final String TAG_SUBMIT = "submit";
  private static final String TAG_CANCEL = "cancel";
  private Context context;
  private TimeType type;

  public CommonTimeDialog(Context context) {
    super(context, R.style.dialog);
    this.type = TimeType.ALL;
    init(context);
  }

  public CommonTimeDialog(Context context, int theme) {
    super(context, theme);
    this.type = TimeType.ALL;
    init(context);
  }

  public CommonTimeDialog(Context context, TimeType type) {
    super(context, R.style.dialog);
    this.type = type;
    init(context);
  }

  /**
   * @param context
   */
  private void init(Context context) {
    this.context = context;
    LayoutInflater mLayoutInflater = LayoutInflater.from(context);
    rootView = mLayoutInflater.inflate(R.layout.dialog_time, null);
    // ----时间转轮
    View wheelView = rootView.findViewById(R.id.include_date);
    Log.d(TAG, "type="+type.toString());
    wheelTime = new WheelTime(wheelView, type);

  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    btnSubmit = (Button) rootView.findViewById(R.id.bt_sure);
    btnSubmit.setBackgroundResource(PickViewConst.BT_CONFIRM_BG);
    btnSubmit.setTextColor(context.getResources().getColorStateList(PickViewConst.BT_CONFIRM_TEXT));
    btnSubmit.setTag(TAG_SUBMIT);

    btnCancel = (Button) rootView.findViewById(R.id.bt_cancle);
    btnCancel.setBackgroundResource(PickViewConst.BT_CANCLE_BG);
    btnCancel.setTextColor(context.getResources().getColorStateList(PickViewConst.BT_CANCLE_TEXT));

    btnCancel.setTag(TAG_CANCEL);

    btnSubmit.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
    
    
    // 默认选中当前时间
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    if (!type.toString().equals(TimeType.CUSTOM_DATE_TIME.toString())) {
      wheelTime.setPicker(year, month, day, hours, minute);
    }
    
    setContentView(rootView);
  }

  /**
   * 设置可以选择的时间范围
   * 
   * @param START_YEAR
   * @param END_YEAR
   */
  public void setRange(int START_YEAR, int END_YEAR) {
    WheelTime.setSTART_YEAR(START_YEAR);
    WheelTime.setEND_YEAR(END_YEAR);
  }
  public void setCustomDate(List<String> monLs, List<String> dayLs, List<String> hourLs, List<String> minLs) {
    wheelTime.setCustomDatePicker(monLs, dayLs, hourLs, minLs);
  }

  /**
   * 设置选中时间
   * 
   * @param date
   */
  public void setTime(Date date) {
    Calendar calendar = Calendar.getInstance();
    if (date == null)
      calendar.setTimeInMillis(System.currentTimeMillis());
    else
      calendar.setTime(date);
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    wheelTime.setPicker(year, month, day, hours, minute);
  }
 
  /**
   * 设置是否循环滚动
   * 
   * @param cyclic
   */
  public void setCyclic(boolean cyclic) {
    wheelTime.setCyclic(cyclic);
  }
  
  
  @Override
  public void onClick(View v) {
    String tag = (String) v.getTag();
    if (tag.equals(TAG_CANCEL)) {
      dismiss();
      return;
    } else {
      dismiss();
    }
  }
  
}
